#include <iostream>
#include <signal.h>
#include "ojcontrol.hpp"
#include "../comm/httplib.h"
using namespace ns_control;
using namespace httplib;

static  Control* g_ctl;
void recovery(int sig)
{
    g_ctl->Recovery();
}
int main()
{
    ::signal(SIGQUIT,recovery);
    //网络服务
    Server svr;
    Control ctl;
    g_ctl = &ctl;
    //用户获取所有题目列表
    svr.Get("/all_questions",[&ctl](const Request& req,Response& resp){
        std::string html;
        ctl.AllQuestions(&html);
        resp.set_content(html,"text/html;charset=utf-8");
    });

    //用户选定了一道题，展示题目编辑页面，(正则表达式和Raw String)
    svr.Get(R"(/question/(\d+))",[&ctl](const Request& req,Response& resp){
        std::string html;
        std::string number = req.matches[1];//Request的matches用来匹配正则表达式
        ctl.OneQuestion(number,&html);
        resp.set_content(html,"text/html;charset=utf-8");
    });

    //用户提交代码，使用我们的判题功能
    svr.Post(R"(/judge/(\d+))",[&ctl](const Request& req,Response& resp){
        std::string number = req.matches[1];//Request的matches用来匹配正则表达式
        std::string html;
        ctl.Judge(number,req.body,&html);
        resp.set_content(html,"application/json;charset=utf-8");
    });
    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0",8088);
    return 0;
}